package com.bookhouse.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.bookhouse.domain.Reader;
import com.bookhouse.domain.ReaderType;
import com.bookhouse.service.ReaderService;
import com.bookhouse.service.ReaderTypeService;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONObject;


public class ReaderAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ReaderService readerService;
	private ReaderTypeService readerTypeService;

	public void setReaderService(ReaderService readerService) {
		this.readerService = readerService;
	}

	public void setReaderTypeService(ReaderTypeService readerTypeService) {
		this.readerTypeService = readerTypeService;
	}

	private transient Reader reader = new Reader();
	private transient List<Reader> list;
	private transient List<ReaderType> readerTypes;
	private transient String readerId;
	private String readerName;
    private transient InputStream inputStream;

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

//    public Reader getReader() {
//		return reader;
//	}
//
//	public void setReader(Reader reader) {
//		this.reader = reader;
//	}


    public Reader getReader() {
        return reader;
    }

    public void setReader(Reader reader) {
        this.reader = reader;
    }

    public List<Reader> getList() {
		return list;
	}

	public void setList(List<Reader> list) {
		this.list = list;
	}

	public List<ReaderType> getReaderTypes() {
		return readerTypes;
	}

	public void setReaderTypes(List<ReaderType> readerTypes) {
		this.readerTypes = readerTypes;
	}

	public String getReaderId() {
		return readerId;
	}

	public void setReaderId(String readerId) {
		this.readerId = readerId;
	}

	public String getReaderName() {
		return readerName;
	}

	public void setReaderName(String readerName) {
		this.readerName = readerName;
	}

    public InputStream getResult(){
        return inputStream;
    }

	public String saveReader() throws Exception {
		readerService.saveReader(reader);
		return "saveReader";
	}

	public String getReaderList() throws Exception {
		list = readerService.getAllReader();
		return "getReaderList";
	}

	public String getReaderEdit() throws Exception {
		if (readerId != null && !readerId.isEmpty()) {
			reader = readerService.getReader(Integer.parseInt(readerId));
		}
		readerTypes = readerTypeService.getAllReaderType();
		return "getReaderEdit";
	}

	public String checkReaderName() throws Exception {
        int count = 0;

		List<Reader> list = readerService.getAllReader();
		for (Reader reader : list) {
			if (reader.getName().equals(readerName)) {
                count++;
			}
		}
        if(count>0) {
            inputStream = new ByteArrayInputStream("success".getBytes());
        }
        System.out.println(readerName);
        System.out.println(count);
        System.out.println(inputStream);
		return "checkReaderName";
	}

    public String getJson(){
        return "getJson";
    }
}
